<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
if($_RQ['op']=='list'){
	$list=pdo_getall('jy_cmsdecorate_company',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','','paixu asc');
	foreach($list as &$item){
		//已支付，已确认，已完成订单数
		$num1=pdo_count('jy_cmsdecorate_order',array('uniacid'=>$_SESSION['uniacid'],'cid'=>$item['id'],'status'=>'1'));
		$num2=pdo_count('jy_cmsdecorate_order',array('uniacid'=>$_SESSION['uniacid'],'cid'=>$item['id'],'status'=>'2'));
		$num3=pdo_count('jy_cmsdecorate_order',array('uniacid'=>$_SESSION['uniacid'],'cid'=>$item['id'],'status'=>'4'));
		$item['num']=$num1+$num2+$num3;
	}
	$return['post']=array();
	$return['list']=$list;
	//print_r($return);exit;
	exi($return);
}
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('没有找到该公司，请重新选择！','error','jy_cmsdecorate/company/list');
	}
	//查询公司具体信息
	$company=pdo_get('jy_cmsdecorate_company',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$looknum=pdo_getcolumn('jy_cmsdecorate_company',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'looknum');
	$looknum=$looknum+1;
	pdo_update('jy_cmsdecorate_company',array('looknum'=>$looknum),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$num1=pdo_count('jy_cmsdecorate_order',array('uniacid'=>$_SESSION['uniacid'],'cid'=>$company['id'],'status'=>'1'));
	$num2=pdo_count('jy_cmsdecorate_order',array('uniacid'=>$_SESSION['uniacid'],'cid'=>$company['id'],'status'=>'2'));
	$num3=pdo_count('jy_cmsdecorate_order',array('uniacid'=>$_SESSION['uniacid'],'cid'=>$company['id'],'status'=>'4'));
	$company['num']=$num1+$num2+$num3;
	//查询本公司的设计师
	$stylist=pdo_getall('jy_cmsdecorate_stylist',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','cid'=>$_RQ['id']));
	
	$this->session('cid',$_RQ['id']);
	//exi(gettype(json_decode($company['advantage'],'true')),'error');
	//$company['advantage']=json_decode($company['advantage'],'true');
	$return['stylist']=$stylist;
	$return['company']=$company;
	$return['post']=array();
	exi($return);
}
if($_RQ['op']=='notice'){
	$postdata=get_postdata();
	$cid = $this->session('cid');
	$stylist=pdo_getall('jy_cmsdecorate_stylist',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','cid'=>$cid));
	$return['cid']=$cid;
	$return['stylist']=$stylist;
	$return['info']=array();
	//print_r($return);exit;
	exi($return);
}
if($_RQ['op']=='case'){
	$cid = $this->session('cid');
	$case=pdo_getall('jy_cmsdecorate_case',array('uniacid'=>$_SESSION['uniacid'],'cid'=>$cid));
	foreach($case as &$v){
		if($v['ishot']){
			$v['ishot']='热门';
		}else{
			$v['ishot']='';
		}
	}
	$return['list']=$case;	
	exi($return);
}
if($_RQ['op']=='submit'){
	$postdata=get_postdata();
	$cid = $this->session('cid');
	$stylist=pdo_getall('jy_cmsdecorate_stylist',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','cid'=>$cid));
	if(!$postdata['name']){
		exi('请输入您的姓名','error');
	}
	if(!$postdata['mobile']){
		exi('请输入您的电话','error');
	}
	if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  
	 
		exi('请输入正确的手机号码','error');
	} 
	if(!$postdata['address']){
		exi('请输入装修地址','error');
	}
	if($postdata){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'user_id'=>$_SESSION['uid'],
			'sid'=>$postdata['sid'],
			'cid'=>$this->session('cid'),
			'address'=>$postdata['address'],
			'remark'=>$postdata['remark'],
			'ordersn'=>get_ordersn('jy_cmsdecorate_order','ordersn'),
			'status'=>1,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_cmsdecorate_order',$insert);
		$id=pdo_insertid();
		$update=array(
			'realname'=>$postdata['name'],
			'mobile'=>$postdata['mobile'],
		);
		pdo_update('core_members',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']));
			exi('提交成功','','jy_cmsdecorate/order/list/status=1');
	}
	$return['cid']=$cid;
	$return['stylist']=$stylist;
	$return['info']=array();
	
	exi($return);
}
